import {
  _decorator,
  BoxCollider2D,
  Collider2D,
  Component,
  Contact2DType,
  ERigidBody2DType,
  game,
  Node,
  RigidBody2D,
} from "cc";
const { ccclass, property } = _decorator;

@ccclass("block")
export class block extends Component {
  start() {
    const collider = this.node.getComponent(BoxCollider2D);

    if (collider) {
      collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }
  }

  update(deltaTime: number) {}

  onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) {
    if (otherCollider.node.name === "ball") {
      game.emit("add-score");
      game.emit("play-zhiyin");

      const collider = this.node.getComponent(BoxCollider2D);
      collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
      this.scheduleOnce(() => {
        //   this.node.destroy();
        this.fall();
      });
    }
  }

  fall() {
    const body = this.node.getComponent(RigidBody2D);
    body.type = ERigidBody2DType.Dynamic;
  }
}
